home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxclip.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  17.9 KB  |  581 lines

  1. /* Copyright (C) 1998, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxclip.c,v 1.3 2000/09/19 19:00:34 lpd Exp $ */
  20. /* Implementation of (path-based) clipping */
  21. #include "gx.h"
  22. #include "gxdevice.h"
  23. #include "gxclip.h"
  24. #include "gxpath.h"
  25. #include "gxcpath.h"
  26.  
  27. /* Define whether to look for vertical clipping regions. */
  28. #define CHECK_VERTICAL_CLIPPING
  29.  
  30. /* ------ Rectangle list clipper ------ */
  31.  
  32. /* Device for clipping with a region. */
  33. /* We forward non-drawing operations, but we must be sure to intercept */
  34. /* all drawing operations. */
  35. private dev_proc_open_device(clip_open);
  36. private dev_proc_fill_rectangle(clip_fill_rectangle);
  37. private dev_proc_copy_mono(clip_copy_mono);
  38. private dev_proc_copy_color(clip_copy_color);
  39. private dev_proc_copy_alpha(clip_copy_alpha);
  40. private dev_proc_fill_mask(clip_fill_mask);
  41. private dev_proc_strip_tile_rectangle(clip_strip_tile_rectangle);
  42. private dev_proc_strip_copy_rop(clip_strip_copy_rop);
  43. private dev_proc_get_clipping_box(clip_get_clipping_box);
  44. private dev_proc_get_bits_rectangle(clip_get_bits_rectangle);
  45.  
  46. /* The device descriptor. */
  47. private const gx_device_clip gs_clip_device =
  48. {std_device_std_body(gx_device_clip, 0, "clipper",
  49.              0, 0, 1, 1),
  50.  {clip_open,
  51.   gx_forward_get_initial_matrix,
  52.   gx_default_sync_output,
  53.   gx_default_output_page,
  54.   gx_default_close_device,
  55.   gx_forward_map_rgb_color,
  56.   gx_forward_map_color_rgb,
  57.   clip_fill_rectangle,
  58.   gx_default_tile_rectangle,
  59.   clip_copy_mono,
  60.   clip_copy_color,
  61.   gx_default_draw_line,
  62.   gx_default_get_bits,
  63.   gx_forward_get_params,
  64.   gx_forward_put_params,
  65.   gx_forward_map_cmyk_color,
  66.   gx_forward_get_xfont_procs,
  67.   gx_forward_get_xfont_device,
  68.   gx_forward_map_rgb_alpha_color,
  69.   gx_forward_get_page_device,
  70.   gx_forward_get_alpha_bits,
  71.   clip_copy_alpha,
  72.   gx_forward_get_band,
  73.   gx_default_copy_rop,
  74.   gx_default_fill_path,
  75.   gx_default_stroke_path,
  76.   clip_fill_mask,
  77.   gx_default_fill_trapezoid,
  78.   gx_default_fill_parallelogram,
  79.   gx_default_fill_triangle,
  80.   gx_default_draw_thin_line,
  81.   gx_default_begin_image,
  82.   gx_default_image_data,
  83.   gx_default_end_image,
  84.   clip_strip_tile_rectangle,
  85.   clip_strip_copy_rop,
  86.   clip_get_clipping_box,
  87.   gx_default_begin_typed_image,
  88.   clip_get_bits_rectangle,
  89.   gx_forward_map_color_rgb_alpha,
  90.   gx_no_create_compositor,
  91.   gx_forward_get_hardware_params,
  92.   gx_default_text_begin,
  93.   gx_default_finish_copydevice
  94.  }
  95. };
  96.  
  97. /* Make a clipping device. */
  98. void
  99. gx_make_clip_translate_device(gx_device_clip * dev, const gx_clip_list * list,
  100.                   int tx, int ty, gs_memory_t *mem)
  101. {
  102.     gx_device_init((gx_device *)dev, (const gx_device *)&gs_clip_device,
  103.            mem, true);
  104.     dev->list = *list;
  105.     dev->translation.x = tx;
  106.     dev->translation.y = ty;
  107. }
  108. void
  109. gx_make_clip_path_device(gx_device_clip * dev, const gx_clip_path * pcpath)
  110. {
  111.     gx_make_clip_device(dev, gx_cpath_list(pcpath));
  112. }
  113.  
  114. /* Define debugging statistics for the clipping loops. */
  115. #ifdef DEBUG
  116. struct stats_clip_s {
  117.     long
  118.          loops, out, in_y, in, in1, down, up, x, no_x;
  119. } stats_clip;
  120. private uint clip_interval = 10000;
  121.  
  122. # define INCR(v) (++(stats_clip.v))
  123. # define INCR_THEN(v, e) (INCR(v), (e))
  124. #else
  125. # define INCR(v) DO_NOTHING
  126. # define INCR_THEN(v, e) (e)
  127. #endif
  128.  
  129. /*
  130.  * Enumerate the rectangles of the x,w,y,h argument that fall within
  131.  * the clipping region.
  132.  */
  133. private int
  134. clip_enumerate_rest(gx_device_clip * rdev,
  135.             int x, int y, int xe, int ye,
  136.             int (*process)(P5(clip_callback_data_t * pccd,
  137.                       int xc, int yc, int xec, int yec)),
  138.             clip_callback_data_t * pccd)
  139. {
  140.     gx_clip_rect *rptr = rdev->current;        /* const within algorithm */
  141.     int yc;
  142.     int code;
  143.  
  144. #ifdef DEBUG
  145.     if (INCR(loops) % clip_interval == 0 && gs_debug_c('q')) {
  146.     dprintf5("[q]loops=%ld out=%ld in_y=%ld in=%ld in1=%ld\n",
  147.          stats_clip.loops, stats_clip.out, stats_clip.in,
  148.          stats_clip.in_y, stats_clip.in1);
  149.     dprintf4("[q]   down=%ld up=%ld x=%ld no_x=%ld\n",
  150.          stats_clip.down, stats_clip.up, stats_clip.x,
  151.          stats_clip.no_x);
  152.     }
  153. #endif
  154.     pccd->x = x, pccd->y = y;
  155.     pccd->w = xe - x, pccd->h = ye - y;
  156.     /*
  157.      * Warp the cursor forward or backward to the first rectangle row
  158.      * that could include a given y value.  Assumes rptr is set, and
  159.      * updates it.  Specifically, after this loop, either rptr == 0 (if
  160.      * the y value is greater than all y values in the list), or y <
  161.      * rptr->ymax and either rptr->prev == 0 or y >= rptr->prev->ymax.
  162.      * Note that y <= rptr->ymin is possible.
  163.      *
  164.      * In the first case below, the while loop is safe because if there
  165.      * is more than one rectangle, there is a 'stopper' at the end of
  166.      * the list.
  167.      */
  168.     if (y >= rptr->ymax) {
  169.     if ((rptr = rptr->next) != 0)
  170.         while (INCR_THEN(up, y >= rptr->ymax))
  171.         rptr = rptr->next;
  172.     } else
  173.     while (rptr->prev != 0 && y < rptr->prev->ymax)
  174.         INCR_THEN(down, rptr = rptr->prev);
  175.     if (rptr == 0 || (yc = rptr->ymin) >= ye) {
  176.     INCR(out);
  177.     if (rdev->list.count > 1)
  178.         rdev->current =
  179.         (rptr != 0 ? rptr :
  180.          y >= rdev->current->ymax ? rdev->list.tail :
  181.          rdev->list.head);
  182.     return 0;
  183.     }
  184.     rdev->current = rptr;
  185.     if (yc < y)
  186.     yc = y;
  187.  
  188.     do {
  189.     const int ymax = rptr->ymax;
  190.     int yec = min(ymax, ye);
  191.  
  192.     if_debug2('Q', "[Q]yc=%d yec=%d\n", yc, yec);
  193.     do {
  194.         int xc = rptr->xmin;
  195.         int xec = rptr->xmax;
  196.  
  197.         if (xc < x)
  198.         xc = x;
  199.         if (xec > xe)
  200.         xec = xe;
  201.         if (xec > xc) {
  202.         clip_rect_print('Q', "match", rptr);
  203.         if_debug2('Q', "[Q]xc=%d xec=%d\n", xc, xec);
  204.         INCR(x);
  205. /*
  206.  * Conditionally look ahead to detect unclipped vertical strips.  This is
  207.  * really only valuable for 90 degree rotated images or (nearly-)vertical
  208.  * lines with convex clipping regions; if we ever change images to use
  209.  * source buffering and destination-oriented enumeration, we could probably
  210.  * take out the code here with no adverse effects.
  211.  */
  212. #ifdef CHECK_VERTICAL_CLIPPING
  213.         if (xec - xc == pccd->w) {    /* full width */
  214.             /* Look ahead for a vertical swath. */
  215.             while ((rptr = rptr->next) != 0 &&
  216.                rptr->ymin == yec &&
  217.                rptr->ymax <= ye &&
  218.                rptr->xmin <= x &&
  219.                rptr->xmax >= xe
  220.                )
  221.             yec = rptr->ymax;
  222.         } else
  223.             rptr = rptr->next;
  224. #else
  225.         rptr = rptr->next;
  226. #endif
  227.         code = process(pccd, xc, yc, xec, yec);
  228.         if (code < 0)
  229.             return code;
  230.         } else {
  231.         INCR_THEN(no_x, rptr = rptr->next);
  232.         }
  233.         if (rptr == 0)
  234.         return 0;
  235.     }
  236.     while (rptr->ymax == ymax);
  237.     } while ((yc = rptr->ymin) < ye);
  238.     return 0;
  239. }
  240.  
  241. private int
  242. clip_enumerate(gx_device_clip * rdev, int x, int y, int w, int h,
  243.            int (*process)(P5(clip_callback_data_t * pccd,
  244.                  int xc, int yc, int xec, int yec)),
  245.            clip_callback_data_t * pccd)
  246. {
  247.     int xe, ye;
  248.     const gx_clip_rect *rptr = rdev->current;
  249.  
  250.     if (w <= 0 || h <= 0)
  251.     return 0;
  252.     pccd->tdev = rdev->target;
  253.     x += rdev->translation.x;
  254.     xe = x + w;
  255.     y += rdev->translation.y;
  256.     ye = y + h;
  257.     /* Check for the region being entirely within the current rectangle. */
  258.     if (y >= rptr->ymin && ye <= rptr->ymax &&
  259.     x >= rptr->xmin && xe <= rptr->xmax
  260.     ) {
  261.     pccd->x = x, pccd->y = y, pccd->w = w, pccd->h = h;
  262.     return INCR_THEN(in, process(pccd, x, y, xe, ye));
  263.     }
  264.     return clip_enumerate_rest(rdev, x, y, xe, ye, process, pccd);
  265. }
  266.  
  267. /* Open a clipping device */
  268. private int
  269. clip_open(gx_device * dev)
  270. {
  271.     gx_device_clip *const rdev = (gx_device_clip *) dev;
  272.     gx_device *tdev = rdev->target;
  273.  
  274.     /* Initialize the cursor. */
  275.     rdev->current =
  276.     (rdev->list.head == 0 ? &rdev->list.single : rdev->list.head);
  277.     rdev->color_info = tdev->color_info;
  278.     rdev->cached_colors = tdev->cached_colors;
  279.     rdev->width = tdev->width;
  280.     rdev->height = tdev->height;
  281.     gx_device_copy_color_procs(dev, tdev);
  282.     rdev->clipping_box_set = false;
  283.     return 0;
  284. }
  285.  
  286. /* Fill a rectangle */
  287. int
  288. clip_call_fill_rectangle(clip_callback_data_t * pccd, int xc, int yc, int xec, int yec)
  289. {
  290.     return (*dev_proc(pccd->tdev, fill_rectangle))
  291.     (pccd->tdev, xc, yc, xec - xc, yec - yc, pccd->color[0]);
  292. }
  293. private int
  294. clip_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  295.             gx_color_index color)
  296. {
  297.     gx_device_clip *rdev = (gx_device_clip *) dev;
  298.     clip_callback_data_t ccdata;
  299.     /* We handle the fastest cases in-line here. */
  300.     gx_device *tdev = rdev->target;
  301.     /*const*/ gx_clip_rect *rptr = rdev->current;
  302.     int xe, ye;
  303.  
  304.     if (w <= 0 || h <= 0)
  305.     return 0;
  306.     x += rdev->translation.x;
  307.     xe = x + w;
  308.     y += rdev->translation.y;
  309.     ye = y + h;
  310.     /* We open-code the most common cases here. */
  311.     if ((y >= rptr->ymin && ye <= rptr->ymax) ||
  312.     ((rptr = rptr->next) != 0 &&
  313.      y >= rptr->ymin && ye <= rptr->ymax)
  314.     ) {
  315.     rdev->current = rptr;    /* may be redundant, but awkward to avoid */
  316.     INCR(in_y);
  317.     if (x >= rptr->xmin && xe <= rptr->xmax) {
  318.         INCR(in);
  319.         return dev_proc(tdev, fill_rectangle)(tdev, x, y, w, h, color);
  320.     }
  321.     else if ((rptr->prev == 0 || rptr->prev->ymax != rptr->ymax) &&
  322.          (rptr->next == 0 || rptr->next->ymax != rptr->ymax)
  323.          ) {
  324.         INCR(in1);
  325.         if (x < rptr->xmin)
  326.         x = rptr->xmin;
  327.         if (xe > rptr->xmax)
  328.         xe = rptr->xmax;
  329.         return
  330.         (x >= xe ? 0 :
  331.          dev_proc(tdev, fill_rectangle)(tdev, x, y, xe - x, h, color));
  332.     }
  333.     }
  334.     ccdata.tdev = tdev;
  335.     ccdata.color[0] = color;
  336.     return clip_enumerate_rest(rdev, x, y, xe, ye,
  337.                    clip_call_fill_rectangle, &ccdata);
  338. }
  339.  
  340. /* Copy a monochrome rectangle */
  341. int
  342. clip_call_copy_mono(clip_callback_data_t * pccd, int xc, int yc, int xec, int yec)
  343. {
  344.     return (*dev_proc(pccd->tdev, copy_mono))
  345.     (pccd->tdev, pccd->data + (yc - pccd->y) * pccd->raster,
  346.      pccd->sourcex + xc - pccd->x, pccd->raster, gx_no_bitmap_id,
  347.      xc, yc, xec - xc, yec - yc, pccd->color[0], pccd->color[1]);
  348. }
  349. private int
  350. clip_copy_mono(gx_device * dev,
  351.            const byte * data, int sourcex, int raster, gx_bitmap_id id,
  352.            int x, int y, int w, int h,
  353.            gx_color_index color0, gx_color_index color1)
  354. {
  355.     gx_device_clip *rdev = (gx_device_clip *) dev;
  356.     clip_callback_data_t ccdata;
  357.     /* We handle the fastest case in-line here. */
  358.     gx_device *tdev = rdev->target;
  359.     const gx_clip_rect *rptr = rdev->current;
  360.     int xe, ye;
  361.  
  362.     if (w <= 0 || h <= 0)
  363.     return 0;
  364.     x += rdev->translation.x;
  365.     xe = x + w;
  366.     y += rdev->translation.y;
  367.     ye = y + h;
  368.     if (y >= rptr->ymin && ye <= rptr->ymax) {
  369.     INCR(in_y);
  370.     if (x >= rptr->xmin && xe <= rptr->xmax) {
  371.         INCR(in);
  372.         return dev_proc(tdev, copy_mono)
  373.         (tdev, data, sourcex, raster, id, x, y, w, h, color0, color1);
  374.     }
  375.     }
  376.     ccdata.tdev = tdev;
  377.     ccdata.data = data, ccdata.sourcex = sourcex, ccdata.raster = raster;
  378.     ccdata.color[0] = color0, ccdata.color[1] = color1;
  379.     return clip_enumerate_rest(rdev, x, y, xe, ye,
  380.                    clip_call_copy_mono, &ccdata);
  381. }
  382.  
  383. /* Copy a color rectangle */
  384. int
  385. clip_call_copy_color(clip_callback_data_t * pccd, int xc, int yc, int xec, int yec)
  386. {
  387.     return (*dev_proc(pccd->tdev, copy_color))
  388.     (pccd->tdev, pccd->data + (yc - pccd->y) * pccd->raster,
  389.      pccd->sourcex + xc - pccd->x, pccd->raster, gx_no_bitmap_id,
  390.      xc, yc, xec - xc, yec - yc);
  391. }
  392. private int
  393. clip_copy_color(gx_device * dev,
  394.         const byte * data, int sourcex, int raster, gx_bitmap_id id,
  395.         int x, int y, int w, int h)
  396. {
  397.     gx_device_clip *rdev = (gx_device_clip *) dev;
  398.     clip_callback_data_t ccdata;
  399.  
  400.     ccdata.data = data, ccdata.sourcex = sourcex, ccdata.raster = raster;
  401.     return clip_enumerate(rdev, x, y, w, h, clip_call_copy_color, &ccdata);
  402. }
  403.  
  404. /* Copy a rectangle with alpha */
  405. int
  406. clip_call_copy_alpha(clip_callback_data_t * pccd, int xc, int yc, int xec, int yec)
  407. {
  408.     return (*dev_proc(pccd->tdev, copy_alpha))
  409.     (pccd->tdev, pccd->data + (yc - pccd->y) * pccd->raster,
  410.      pccd->sourcex + xc - pccd->x, pccd->raster, gx_no_bitmap_id,
  411.      xc, yc, xec - xc, yec - yc, pccd->color[0], pccd->depth);
  412. }
  413. private int
  414. clip_copy_alpha(gx_device * dev,
  415.         const byte * data, int sourcex, int raster, gx_bitmap_id id,
  416.         int x, int y, int w, int h,
  417.         gx_color_index color, int depth)
  418. {
  419.     gx_device_clip *rdev = (gx_device_clip *) dev;
  420.     clip_callback_data_t ccdata;
  421.  
  422.     ccdata.data = data, ccdata.sourcex = sourcex, ccdata.raster = raster;
  423.     ccdata.color[0] = color, ccdata.depth = depth;
  424.     return clip_enumerate(rdev, x, y, w, h, clip_call_copy_alpha, &ccdata);
  425. }
  426.  
  427. /* Fill a region defined by a mask. */
  428. int
  429. clip_call_fill_mask(clip_callback_data_t * pccd, int xc, int yc, int xec, int yec)
  430. {
  431.     return (*dev_proc(pccd->tdev, fill_mask))
  432.     (pccd->tdev, pccd->data + (yc - pccd->y) * pccd->raster,
  433.      pccd->sourcex + xc - pccd->x, pccd->raster, gx_no_bitmap_id,
  434.      xc, yc, xec - xc, yec - yc, pccd->pdcolor, pccd->depth,
  435.      pccd->lop, NULL);
  436. }
  437. private int
  438. clip_fill_mask(gx_device * dev,
  439.            const byte * data, int sourcex, int raster, gx_bitmap_id id,
  440.            int x, int y, int w, int h,
  441.            const gx_drawing_color * pdcolor, int depth,
  442.            gs_logical_operation_t lop, const gx_clip_path * pcpath)
  443. {
  444.     gx_device_clip *rdev = (gx_device_clip *) dev;
  445.     clip_callback_data_t ccdata;
  446.  
  447.     if (pcpath != 0)
  448.     return gx_default_fill_mask(dev, data, sourcex, raster, id,
  449.                     x, y, w, h, pdcolor, depth, lop,
  450.                     pcpath);
  451.     ccdata.data = data, ccdata.sourcex = sourcex, ccdata.raster = raster;
  452.     ccdata.pdcolor = pdcolor, ccdata.depth = depth, ccdata.lop = lop;
  453.     return clip_enumerate(rdev, x, y, w, h, clip_call_fill_mask, &ccdata);
  454. }
  455.  
  456. /* Strip-tile a rectangle. */
  457. int
  458. clip_call_strip_tile_rectangle(clip_callback_data_t * pccd, int xc, int yc, int xec, int yec)
  459. {
  460.     return (*dev_proc(pccd->tdev, strip_tile_rectangle))
  461.     (pccd->tdev, pccd->tiles, xc, yc, xec - xc, yec - yc,
  462.      pccd->color[0], pccd->color[1], pccd->phase.x, pccd->phase.y);
  463. }
  464. private int
  465. clip_strip_tile_rectangle(gx_device * dev, const gx_strip_bitmap * tiles,
  466.               int x, int y, int w, int h,
  467.      gx_color_index color0, gx_color_index color1, int phase_x, int phase_y)
  468. {
  469.     gx_device_clip *rdev = (gx_device_clip *) dev;
  470.     clip_callback_data_t ccdata;
  471.  
  472.     ccdata.tiles = tiles;
  473.     ccdata.color[0] = color0, ccdata.color[1] = color1;
  474.     ccdata.phase.x = phase_x, ccdata.phase.y = phase_y;
  475.     return clip_enumerate(rdev, x, y, w, h, clip_call_strip_tile_rectangle, &ccdata);
  476. }
  477.  
  478. /* Copy a rectangle with RasterOp and strip texture. */
  479. int
  480. clip_call_strip_copy_rop(clip_callback_data_t * pccd, int xc, int yc, int xec, int yec)
  481. {
  482.     return (*dev_proc(pccd->tdev, strip_copy_rop))
  483.     (pccd->tdev, pccd->data + (yc - pccd->y) * pccd->raster,
  484.      pccd->sourcex + xc - pccd->x, pccd->raster, gx_no_bitmap_id,
  485.      pccd->scolors, pccd->textures, pccd->tcolors,
  486.      xc, yc, xec - xc, yec - yc, pccd->phase.x, pccd->phase.y,
  487.      pccd->lop);
  488. }
  489. private int
  490. clip_strip_copy_rop(gx_device * dev,
  491.           const byte * sdata, int sourcex, uint raster, gx_bitmap_id id,
  492.             const gx_color_index * scolors,
  493.        const gx_strip_bitmap * textures, const gx_color_index * tcolors,
  494.             int x, int y, int w, int h,
  495.             int phase_x, int phase_y, gs_logical_operation_t lop)
  496. {
  497.     gx_device_clip *rdev = (gx_device_clip *) dev;
  498.     clip_callback_data_t ccdata;
  499.  
  500.     ccdata.data = sdata, ccdata.sourcex = sourcex, ccdata.raster = raster;
  501.     ccdata.scolors = scolors, ccdata.textures = textures,
  502.     ccdata.tcolors = tcolors;
  503.     ccdata.phase.x = phase_x, ccdata.phase.y = phase_y, ccdata.lop = lop;
  504.     return clip_enumerate(rdev, x, y, w, h, clip_call_strip_copy_rop, &ccdata);
  505. }
  506.  
  507. /* Get the (outer) clipping box, in client coordinates. */
  508. private void
  509. clip_get_clipping_box(gx_device * dev, gs_fixed_rect * pbox)
  510. {
  511.     gx_device_clip *const rdev = (gx_device_clip *) dev;
  512.  
  513.     if (!rdev->clipping_box_set) {
  514.     gx_device *tdev = rdev->target;
  515.     gs_fixed_rect tbox;
  516.  
  517.     (*dev_proc(tdev, get_clipping_box)) (tdev, &tbox);
  518.     if (rdev->list.count != 0) {
  519.         gs_fixed_rect cbox;
  520.  
  521.         if (rdev->list.count == 1) {
  522.         cbox.p.x = int2fixed(rdev->list.single.xmin);
  523.         cbox.p.y = int2fixed(rdev->list.single.ymin);
  524.         cbox.q.x = int2fixed(rdev->list.single.xmax);
  525.         cbox.q.y = int2fixed(rdev->list.single.ymax);
  526.         } else {
  527.         /* The head and tail elements are dummies.... */
  528.         cbox.p.x = int2fixed(rdev->list.xmin);
  529.         cbox.p.y = int2fixed(rdev->list.head->next->ymin);
  530.         cbox.q.x = int2fixed(rdev->list.xmax);
  531.         cbox.q.y = int2fixed(rdev->list.tail->prev->ymax);
  532.         }
  533.         rect_intersect(tbox, cbox);
  534.     }
  535.     if (rdev->translation.x | rdev->translation.y) {
  536.         fixed tx = int2fixed(rdev->translation.x),
  537.         ty = int2fixed(rdev->translation.y);
  538.  
  539.         if (tbox.p.x != min_fixed)
  540.         tbox.p.x -= tx;
  541.         if (tbox.p.y != min_fixed)
  542.         tbox.p.y -= ty;
  543.         if (tbox.q.x != max_fixed)
  544.         tbox.q.x -= tx;
  545.         if (tbox.q.y != max_fixed)
  546.         tbox.q.y -= ty;
  547.     }
  548.     rdev->clipping_box = tbox;
  549.     rdev->clipping_box_set = true;
  550.     }
  551.     *pbox = rdev->clipping_box;
  552. }
  553.  
  554. /* Get bits back from the device. */
  555. private int
  556. clip_get_bits_rectangle(gx_device * dev, const gs_int_rect * prect,
  557.             gs_get_bits_params_t * params, gs_int_rect ** unread)
  558. {
  559.     gx_device_clip *rdev = (gx_device_clip *) dev;
  560.     gx_device *tdev = rdev->target;
  561.     int tx = rdev->translation.x, ty = rdev->translation.y;
  562.     gs_int_rect rect;
  563.     int code;
  564.  
  565.     rect.p.x = prect->p.x - tx, rect.p.y = prect->p.y - ty;
  566.     rect.q.x = prect->q.x - tx, rect.q.y = prect->q.y - ty;
  567.     code = (*dev_proc(tdev, get_bits_rectangle))
  568.     (tdev, &rect, params, unread);
  569.     if (code > 0) {
  570.     /* Adjust unread rectangle coordinates */
  571.     gs_int_rect *list = *unread;
  572.     int i;
  573.  
  574.     for (i = 0; i < code; ++list, ++i) {
  575.         list->p.x += tx, list->p.y += ty;
  576.         list->q.x += tx, list->q.y += ty;
  577.     }
  578.     }
  579.     return code;
  580. }
  581.